import {getGoods,collects} from "../../server/goods"
import {addCarts} from "../../server/carts"
import toast from "../../miniprogram_npm/@vant/weapp/toast/toast"
import {getCratList} from "../../server/carts"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId:"",
    goods:{},
    likeGoods:[],
    cratList:[],
    showGoodSdetails:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id);
    this.setData({
      goodsId:options.id,
    })
    this.getThisGoods()
    // if()
    if (wx.getStorageSync('access_token')) this.getCratList()
  },
  getThisGoods(){
    return new Promise((resolve,reject) => {
      getGoods(this.data.goodsId).then(res => {
        wx.setNavigationBarTitle({
          title: res.goods.title
        })
        // console.log(res);
        this.setData({
          goods:res.goods,
          likeGoods:res.like_goods,
          showGoodSdetails:res.goods.details.replace(/<img/gi,'<img style="max-width:100% !important; height : auto !important"')
        })
        resolve()
      })
    })
  },
  goIndex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  goCart(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  collection(){
    collects(this.data.goodsId).then(res => {
      // console.log(res);
      this.getThisGoods().then(() => {
        let msg = this.data.goods.is_collect == 0?'取消收藏':'收藏成功'
        toast.success(msg)
      })

      // console.log(this.data.goods.is_collect);
    })
  },
  getAddCrat(){
    let data = {
      goods_id:this.data.goodsId
    }
    addCarts(data).then(res => {
      // console.log(res);
      toast.success("添加成功")
      this.getCratList()
    })
  },
  getCratList(){
    getCratList().then(res =>{
      // console.log(res)
      this.setData({
        cratList:res.data
      }
      )
    })
  }
})